﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace IMark.DAO
{
    public class LoaiSanPhamModels
    {
        public static List<LOAI_SAN_PHAM> GetAll()
        {
            using (var context = new EMartEntities())
            {
                return context.LOAI_SAN_PHAM.ToList();
            }
        }
        public static bool Update(LOAI_SAN_PHAM loaiSanPham)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    LOAI_SAN_PHAM existing = context.LOAI_SAN_PHAM.Find(loaiSanPham.ID);
                    existing.DacBiet = loaiSanPham.DacBiet;
                    existing.MaLoaiCha = loaiSanPham.MaLoaiCha;
                    existing.MetaDescription = loaiSanPham.MetaDescription;
                    existing.MetaKeyword = loaiSanPham.MetaKeyword;
                    existing.MetaTitle = loaiSanPham.MetaTitle;
                    existing.MoTa = loaiSanPham.MoTa;
                    existing.NgaySua = DateTime.Now;
                    existing.NgayTao = loaiSanPham.NgayTao;
                    existing.TenLoai = loaiSanPham.TenLoai;
                    existing.TrangThai = loaiSanPham.TrangThai;
                    existing.HinhAnh = loaiSanPham.HinhAnh;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static LOAI_SAN_PHAM GetByID(long ID)
        {
            using (var context = new EMartEntities())
            {
                return context.LOAI_SAN_PHAM.Find(ID);
            }
        }        
        private static List<LOAI_SAN_PHAM> GetParent()
        {
            using(var context = new EMartEntities())
            {                
                return context.LOAI_SAN_PHAM.Where(u => u.MaLoaiCha == 0).ToList();
            }
        }
        private static List<LOAI_SAN_PHAM> GetChild()
        {
            using (var context = new EMartEntities())
            {
                return context.LOAI_SAN_PHAM.Where(u => u.MaLoaiCha != 0).ToList();
            }
        }
        private static string ShowSelectChild(long maLoaiCha, List<LOAI_SAN_PHAM> loaiCon, string cap)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < loaiCon.Count(); i++)
            {
                if (loaiCon[i].MaLoaiCha == maLoaiCha)
                {                    
                    result.Append("<option value='" + loaiCon[i].ID + "'>" + cap + loaiCon[i].TenLoai + "</option>");
                    result.Append(ShowSelectChild(loaiCon[i].ID, loaiCon, cap + "----"));                    
                }
            }
            return result.ToString();
        }

        public static string ShowSelectProductType(bool noParent = false)
        {
            StringBuilder result = new StringBuilder();
            if (noParent == false)
                result.Append("<select class='form-control' id='productType' name='productType'><option value='0'>No Parent</option>");
            else
                result.Append("<select class='form-control' id='productType' name='productType'>");
            foreach (var loaiCon in GetParent())
            {                
                result.Append("<option value='" + loaiCon.ID + "'>");
                result.Append(loaiCon.TenLoai);
                result.Append("</option>");
                result.Append(ShowSelectChild(loaiCon.ID, GetChild(), "=="));
            }
            result.Append("</select>");
            return result.ToString();            
        }

        public static string ShowSelectProductTypeCheck(long IDLoaiSanPham)
        {
            StringBuilder result = new StringBuilder("<select class='form-control' id='productType' name='productType'>");
            foreach (var loaiCon in GetParent())
            {
                if (loaiCon.ID == IDLoaiSanPham)
                {
                    result.Append("<option selected='selected' value='" + loaiCon.ID + "'>");
                    result.Append(loaiCon.TenLoai);
                    result.Append("</option>");
                }
                else
                {
                    result.Append("<option value='" + loaiCon.ID + "'>");
                    result.Append(loaiCon.TenLoai);
                    result.Append("</option>");
                }
                result.Append(ShowSelectChildCheck(loaiCon.ID, GetChild(), "==", IDLoaiSanPham));
            }
            result.Append("</select>");
            return result.ToString();
        }
        private static string ShowSelectChildCheck(long maLoaiCha, List<LOAI_SAN_PHAM> loaiCon, string cap, long selID)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < loaiCon.Count(); i++)
            {
                if (loaiCon[i].MaLoaiCha == maLoaiCha)
                {
                    if (loaiCon[i].ID == selID)
                    {
                        result.Append("<option selected='selected' value='" + loaiCon[i].ID + "'>");
                        result.Append(cap + loaiCon[i].TenLoai);
                        result.Append("</option>");
                    }
                    else
                    {
                        result.Append("<option value='" + loaiCon[i].ID + "'>" + cap + loaiCon[i].TenLoai);
                        result.Append("</option>");
                    }

                    result.Append(ShowSelectChildCheck(loaiCon[i].ID, loaiCon, cap + "----", selID));
                }
            }
            return result.ToString();
        }


        public static string GetParentName(long IDParent)
        {
            if(IDParent == 0)
            {
                return "0**NoParent**0";
            }
            else
            {
                using (var context = new EMartEntities())
                {
                    if (context.LOAI_SAN_PHAM.Where(u => u.ID == IDParent).FirstOrDefault() == null)
                    {
                        return "UnKnown";
                    }
                    else
                    {
                        return context.LOAI_SAN_PHAM.Where(u => u.ID == IDParent).FirstOrDefault().TenLoai;
                    }
                }
            }
        }
        public static bool CheckDeleteTypeProduct(long ID)
        {
            using (var context = new EMartEntities())
            {
                LOAI_SAN_PHAM loaiSanPham = context.LOAI_SAN_PHAM.Where(u => u.MaLoaiCha == ID).FirstOrDefault();
                if (loaiSanPham == null)
                    return true;
                else
                    return false;
            }
        }
        public static bool Add(LOAI_SAN_PHAM loaiSanPham)
        {
            using(var context = new EMartEntities())
            {
                try
                {
                    context.LOAI_SAN_PHAM.Add(loaiSanPham);
                    context.SaveChanges();
                    return true;
                }
                catch(Exception)
                {
                    return false;
                }                
            }
        }
        public static bool Delete(long ID)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    LOAI_SAN_PHAM loaiSanPham = context.LOAI_SAN_PHAM.Find(ID);
                    context.LOAI_SAN_PHAM.Remove(loaiSanPham);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool UpdateStatus(int ID, bool Status)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    LOAI_SAN_PHAM existing = context.LOAI_SAN_PHAM.Find(ID);
                    if (Status == true)
                        existing.TrangThai = 0;
                    else
                        existing.TrangThai = 1;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool UpdateStatusHome(int ID, bool Status)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    LOAI_SAN_PHAM existing = context.LOAI_SAN_PHAM.Find(ID);
                    if (Status == true)
                        existing.DacBiet = 0;
                    else
                        existing.DacBiet = 1;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public static List<LOAI_SAN_PHAM> LayLoaiSanPham()
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                return context.LOAI_SAN_PHAM.Where(w=>w.TrangThai==1).ToList();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }

        public static List<LOAI_SAN_PHAM> LayLoaiSanPhamTrangChu()
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                return context.LOAI_SAN_PHAM.Include("SAN_PHAM").Where(w => w.DacBiet == 1 && w.TrangThai==1).ToList();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }

        public static List<LOAI_SAN_PHAM> LayLoaiSanPhamLienQuan(long LoaiSP)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    List<LOAI_SAN_PHAM> ltLoaiSP = new List<LOAI_SAN_PHAM>();
                    long IDCha = context.LOAI_SAN_PHAM.Single(w => w.ID == LoaiSP).MaLoaiCha;
                    var queryListMaCha = context.LOAI_SAN_PHAM.Include("SAN_PHAM").Where(w => w.MaLoaiCha == IDCha && w.DacBiet==1).ToList();
                    foreach (var item in queryListMaCha)
                    {
                        ltLoaiSP.Add(new LOAI_SAN_PHAM()
                        {
                            DacBiet = item.DacBiet,
                            ID = item.ID,
                            MaLoaiCha = item.MaLoaiCha,
                            MetaDescription = item.MetaDescription,
                            MetaKeyword = item.MetaKeyword,
                            MetaTitle = item.MetaTitle,
                            MoTa = item.MoTa,
                            NgaySua = item.NgaySua,
                            NgayTao = item.NgayTao,
                            TenLoai = item.TenLoai,
                            TrangThai = item.TrangThai,
                            SAN_PHAM = item.SAN_PHAM
                        });
                        var LoaiCon = context.LOAI_SAN_PHAM.Include("SAN_PHAM").Where(w => w.MaLoaiCha == item.ID);
                        foreach (var itemCon in LoaiCon)
                        {
                            ltLoaiSP.Add(new LOAI_SAN_PHAM()
                            {
                                DacBiet = itemCon.DacBiet,
                                ID = itemCon.ID,
                                MaLoaiCha = itemCon.MaLoaiCha,
                                MetaDescription = itemCon.MetaDescription,
                                MetaKeyword = itemCon.MetaKeyword,
                                MetaTitle = itemCon.MetaTitle,
                                MoTa = itemCon.MoTa,
                                NgaySua = itemCon.NgaySua,
                                NgayTao = itemCon.NgayTao,
                                TenLoai = itemCon.TenLoai,
                                TrangThai = itemCon.TrangThai,
                                SAN_PHAM = itemCon.SAN_PHAM
                            });
                        }
                    }
                    return ltLoaiSP;
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
    }
}